package com.david.system.mapper;

import com.david.system.pojo.Entity.AuthorityEntity;
import com.david.system.pojo.Entity.MenuEntity;
import com.david.system.pojo.Entity.RoleEntity;
import com.david.system.pojo.Entity.UserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface AuthorityMapper {

    // 通过用户名查询角色名称，提供角色列表作为权限列表，进行测试
    @Select("select su.username as username,su.id as id, su.dept_id as dept_id, sr.role_code as AuthorityName from sys_user su left join sys_role_user sru on su.id = sru.user_id left join sys_role sr on sru.role_id = sr.id where su.username = #{username}")
    List<AuthorityEntity> selectAuthorityByUsername(String username);

    @Select("select id, name, parent_id, path, type, sort, create_time, update_time, component, permission, icon from sys_menu")
    List<MenuEntity> selectMenuAll();

    @Select("select sr.id, sr.role_code, sr.role_name, sr.create_time, sr.description, sr.update_time  from sys_role sr left join sys_role_menu srm on sr.id = srm.role_id where srm.menu_id = #{id}")
    List<RoleEntity> selectRoleCodeByMenuId(Integer id);

    @Select("select id, email, username, password, create_time, enabled, name, update_time, gender, code, phone, status, dept_id, job_id from sys_user where username = #{username}")
    UserEntity selectUserByUsername(String username);

    @Update("update sys_user set password = #{encodePassword} where username = #{username}")
    void updatePassword(@Param("encodePassword") String encodePassword,@Param("username") String username);
}
